<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <!-- 主体区域绘制 -->
        <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
          <template slot-scope="{ data }">
            <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹出框 -->
        <el-dialog
          :title="isEdit?'编辑':'添加'"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :visible.sync="showDialog"
          width="50%"
        >
          <dept-dialog v-if="showDialog" :origin-list="originList" :parent-id="parentId" :is-edit="isEdit" @close="updateDepart" />

        </el-dialog>
      </el-card>

    </div>
  </div>
</template>

<script>
import DeptDialog from './deptDialog.vue'
import { getDepartments } from '@/api/departments'
import { transTree } from '@/utils/index'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      // tads高亮
      activeName: 'first',
      //  tree 数组
      treeData: [],

      defaultProps: {
        children: 'children',
        label: 'name'
      },
      //   控制弹框显示隐藏
      showDialog: false,
      parentId: '', // 父id
      isEdit: true, // 判断是否是编辑 true是 false 是新增
      originList: []
    }
  },
  created() {
    //   初始化挂载
    this.getDepartments()
  },
  methods: {
    updateDepart() {
      this.showDialog = false
      this.getDepartments()
    },

    // 编辑
    editDept(id) {
      // 传递pid
      this.parentId = id
      //   显示弹出框
      this.showDialog = true
      //   是编辑功能
      this.isEdit = true
    },

    // 删除
    delDept(id) {},

    //   添加
    addDept(id) {
      this.parentId = id
      this.showDialog = true
      //   false是添加部门
      this.isEdit = false
    },
    //   组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      //   console.log(res)
      const originList = res.data.depts.map(item => ({
        name: item.name,
        pid: item.pid,
        id: item.id,
        code: item.code
      }))
      this.originList = originList
      this.treeData = transTree(res.data.depts, '')
    //   console.log(transTree(res.data.depts, ''))
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.el-dialog__header {
    background-color: #66b1ff;
}

</style>
